package com.gzc.just.play.last.war.allinone;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Last War All-in-One Server Application
 * 整合所有服务器模块到单一应用程序中
 * 
 * 在此模式下，以下服务器将自动启动：
 * - LoginServer（端口 30001）：用户认证和登录【必需的核心服务】
 * - GateServer（端口 40001）：网关，连接转发【必需的核心服务】
 * - SceneServer（端口 50001）：场景和战斗管理【必需的核心服务】
 * - CentralServer（端口 6001）：中央服务器，状态同步【必需的核心服务】
 * - BalanceServer（端口 7001）：负载均衡【必需的核心服务】
 * - RobotClient：机器人客户端，用于压力测试【可选】
 * 
 * HTTP服务在端口 8088 上运行
 */
@SpringBootApplication(exclude = {
    RedisAutoConfiguration.class,
    RedisRepositoriesAutoConfiguration.class
})
@ComponentScan(basePackages = {
    "com.gzc.just.play.last.war.allinone",        // all-in-one模块
    "com.gzc.just.play.last.war.sceneserver",     // scene-server模块
    "com.gzc.just.play.last.war.gateserver",      // gate-server模块
    "com.gzc.just.play.last.war.loginserver",     // login-server模块
    "com.gzc.just.play.last.war.centralserver",   // central-server模块
    "com.gzc.just.play.last.war.balanceserver",   // balance-server模块
    "com.gzc.just.play.last.war.common",          // common模块
    "com.gzc.just.play.last.war.robotclient",     // robot-client模块
})
@EnableScheduling
public class AllInOneApplication {
    private static final Logger log = LoggerFactory.getLogger(AllInOneApplication.class);

    public static void main(String[] args) {
        log.info("============================================================");
        log.info("Starting Last War All-in-One Server Application");
        log.info("All-in-One Mode: SceneServer and BalanceServer are REQUIRED");
        log.info("Application will bind to 127.0.0.1:8088 (HTTP)");
        log.info("============================================================");
        
        try {
            // 设置默认profile
            System.setProperty("spring.profiles.active", "dev-all-in-one");
            
            // 创建data目录（如果不存在）
            createDataDirectory();
            
            // 启动Spring Boot应用
            SpringApplication.run(AllInOneApplication.class, args);
            
            log.info("All-in-One Server Application started successfully!");
            log.info("All required server modules (SceneServer, BalanceServer) should now be running!");
            
        } catch (Exception e) {
            log.error("Failed to start All-in-One Server Application", e);
            System.exit(1);
        }
    }
    
    /**
     * 创建data目录（如果不存在）
     */
    private static void createDataDirectory() {
        try {
            File dataDir = new File("./data");
            if (!dataDir.exists()) {
                boolean created = dataDir.mkdirs();
                if (created) {
                    log.info("Data directory created successfully: {}", dataDir.getAbsolutePath());
                } else {
                    log.error("Failed to create data directory: {}", dataDir.getAbsolutePath());
                }
            } else {
                log.info("Data directory already exists: {}", dataDir.getAbsolutePath());
            }
            
            // 同时创建logs目录
            File logsDir = new File("./logs");
            if (!logsDir.exists()) {
                boolean created = logsDir.mkdirs();
                if (created) {
                    log.info("Logs directory created successfully: {}", logsDir.getAbsolutePath());
                } else {
                    log.error("Failed to create logs directory: {}", logsDir.getAbsolutePath());
                }
            } else {
                log.info("Logs directory already exists: {}", logsDir.getAbsolutePath());
            }
        } catch (Exception e) {
            log.error("Error creating directories", e);
        }
    }
}